package cn.neu.leon.local;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Dom4Xml {
	public static void main(String arge[]) {
		//long lasting = System.currentTimeMillis();
		try {
			File f = new File("E:library/weibo/1009去重后.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(f);
			NodeList nl = doc.getElementsByTagName("microblog");
			
			
//			for(int i=470800;i<470853;i++)
//			{
//				System.out.println(nl.item(27077).getTextContent());
//				System.out.println(nl.item(27077).getChildNodes().item(3).getNodeName());
//			}
		    System.out.println(nl.getLength());
		    
			for (int i = 12; i < 126850; i++) {
				String textString =  doc.getElementsByTagName("text").item(i)
				.getFirstChild().getNodeValue();
				System.out.println("id："
						+ doc.getElementsByTagName("id").item(i)
								.getFirstChild().getNodeValue());
				System.out.println("name："
						+ doc.getElementsByTagName("name").item(i)
								.getFirstChild().getNodeValue());
				System.out.println("text："
						+ doc.getElementsByTagName("text").item(i)
								.getFirstChild().getNodeValue());
			
				System.out.println("time："
						+ doc.getElementsByTagName("time").item(i)
								.getFirstChild().getNodeValue());
				
//				/*去除微博中的\t\n\r，防止分词停止*/
//				doc.getElementsByTagName("text").item(i)
//				.getFirstChild().setNodeValue(textString.replaceAll("\r|\n|\t",""));
//						System.out.println("prob："
//								+ doc.getElementsByTagName("prob").item(i)
//										.getFirstChild().getNodeValue());
			}
//			/** 以下将修改后的DOM对象存入xml文件中 */
//			TransformerFactory tFactory = TransformerFactory.newInstance();
//			Transformer transformer = tFactory.newTransformer();
//			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 编码
//			DOMSource source = new DOMSource(doc);
//			StreamResult result = new StreamResult(
//					new File("./source/1031去重后.xml"));
//			transformer.transform(source, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

